﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crownwood.Magic.Controls;
using ConnectSQLServer.Controls;
using ConnectSQLServer.Common;

namespace ConnectSQLServer.Manager
{
    public class QueryManager
    {
        public void NewQuery()
        {
            Crownwood.Magic.Controls.TabPage tp = new Crownwood.Magic.Controls.TabPage("New Query_" + GlobalVariable.iNewQuery);
            tp.Name = "Editor";
            tp.Tag = "Save";
            Editor newEditor = new Editor();
            newEditor.txtEditingArea.Document.HighlightingStrategy = ICSharpCode.TextEditor.Document.HighlightingStrategyFactory.CreateHighlightingStrategy("SQL");
            newEditor.ContextMenuStrip = GlobalVariable.contextMenuEditor;
            newEditor.Location = new System.Drawing.Point(0, 0);
            newEditor.Size = new System.Drawing.Size(GlobalVariable.widthPanel2, GlobalVariable.heightPanel2-GlobalVariable.padding);
            tp.Controls.Add(newEditor);
            MyDataTable dt = new MyDataTable();
            dt.Location = new System.Drawing.Point(0, GlobalVariable.heightPanel2 / 2);
            dt.Size = new System.Drawing.Size(0,0);
            tp.Controls.Add(dt);
            GlobalVariable.TCManager.AddTabPage(tp);
            GlobalVariable.iNewQuery++;   
        }

        public void OpenFileQuery(string pathname, string filename)
        {
            //Read file content;
            Crownwood.Magic.Controls.TabPage tp = new Crownwood.Magic.Controls.TabPage(filename);
            tp.Name = "Editor";
            tp.Tag = "Save";
            string text = System.IO.File.ReadAllText(pathname);
            Editor newEditor = new Editor();
            newEditor.txtEditingArea.Document.HighlightingStrategy = ICSharpCode.TextEditor.Document.HighlightingStrategyFactory.CreateHighlightingStrategy("SQL");
            newEditor.txtEditingArea.Document.TextContent = text;
            newEditor.ContextMenuStrip = GlobalVariable.contextMenuEditor;
            newEditor.Location = new System.Drawing.Point(0, 0);
            newEditor.Size = new System.Drawing.Size(GlobalVariable.widthPanel2, GlobalVariable.heightPanel2 - GlobalVariable.padding);
            tp.Controls.Add(newEditor);
            MyDataTable dt = new MyDataTable();
            dt.Location = new System.Drawing.Point(0, GlobalVariable.heightPanel2 / 2);
            dt.Size = new System.Drawing.Size(0, 0);
            tp.Controls.Add(dt);
            GlobalVariable.TCManager.AddTabPage(tp);
        }

        public string GetContentFromEditor()
        {
            TabPage tabPage =  GlobalVariable.TCManager.GetTabPageSelected();
            if (tabPage != null && tabPage.Tag.ToString() == "Editor")
                return ((Editor)tabPage.Control).txtEditingArea.Document.TextContent;
            else
                return string.Empty;
        }

        public void Cut()
        {
            TabPage tabPage = GlobalVariable.TCManager.GetTabPageSelected();
            if (tabPage != null && tabPage.Tag.ToString() == "Editor")
                new ICSharpCode.TextEditor.Actions.Cut().Execute(((Editor)tabPage.Controls[0]).txtEditingArea);
        }
        public void Copy()
        {
            TabPage tabPage = GlobalVariable.TCManager.GetTabPageSelected();
            if (tabPage != null && tabPage.Tag.ToString() == "Editor")
                new ICSharpCode.TextEditor.Actions.Copy().Execute(((Editor)tabPage.Controls[0]).txtEditingArea);
        }
        public void Paste()
        {
            TabPage tabPage = GlobalVariable.TCManager.GetTabPageSelected();
            if (tabPage != null && tabPage.Tag.ToString() == "Editor")
                new ICSharpCode.TextEditor.Actions.Paste().Execute(((Editor)tabPage.Controls[0]).txtEditingArea);
        }
        public void Undo()
        {
            TabPage tabPage = GlobalVariable.TCManager.GetTabPageSelected();
            if (tabPage != null && tabPage.Tag.ToString() == "Editor")
                new ICSharpCode.TextEditor.Actions.Undo().Execute(((Editor)tabPage.Controls[0]).txtEditingArea);
        }
        public void Redo()
        {
            TabPage tabPage = GlobalVariable.TCManager.GetTabPageSelected();
            if (tabPage != null && tabPage.Tag.ToString() == "Editor")
                new ICSharpCode.TextEditor.Actions.Redo().Execute(((Editor)tabPage.Controls[0]).txtEditingArea);
        }

    }
}
